package com.neuedu.controller;


import com.neuedu.pojo.Protocol;
import com.neuedu.service.IProtocolService;
import com.neuedu.util.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author han
 * @since 2020-09-21
 */
@RestController
@RequestMapping("/protocol")
public class ProtocolController {
    @Resource
    IProtocolService protocolService;

    @GetMapping("/list")
    CommonResult list(Protocol protocol) {
        return CommonResult.success(protocolService.list(protocol));
    }
    @PostMapping("/add")
    CommonResult add(Protocol protocol) {
        protocol.setActive(1);
        if(protocolService.add(protocol))
            return CommonResult.success("添加协议成功");
        else
            return CommonResult.failed("此协议已经存在");
    }
    @GetMapping("/getById")
    CommonResult getById(Integer id) {
        return CommonResult.success(protocolService.getById(id));
    }
    @PostMapping("/update")
    CommonResult update(Protocol protocol) {
        LocalDateTime now =LocalDateTime.now();
        protocol.setAlterTime(now);
        return CommonResult.success(protocolService.updateById(protocol));
    }
    @PostMapping("/del")
    CommonResult del(Protocol protocol) {
        protocol.setActive(0);
        return CommonResult.success(protocolService.updateById(protocol));
    }
    @PostMapping("/batchdel")
    CommonResult batchdel(Integer[] ids) {
        return CommonResult.success(protocolService.batchdel(ids));
    }
    @PostMapping("/back")
    CommonResult back(Protocol protocol) {
        protocol.setActive(1);
        return CommonResult.success(protocolService.updateById(protocol));
    }

}
